package com.ssn.controller;

import com.ssn.po.Tag;
import com.ssn.po.Type;
import com.ssn.service.impl.BlogServiceImpl;
import com.ssn.service.impl.TagServiceImpl;
import com.ssn.service.impl.TypeServiceImpl;
import com.ssn.vo.BlogQuery;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class TagBlogController {

    @Resource
    private TagServiceImpl tagService;
    @Resource
    private BlogServiceImpl blogService;

    @GetMapping("/tag/{id}")
    public String tag(@PageableDefault(size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                       @PathVariable Long id, Model model){
        List<Tag> tags = tagService.listTagTop(100);
        Long activeTagId;
        if (id == -1){
            activeTagId = tags.get(0).getId();
        }else{
            activeTagId = id;
        }
        model.addAttribute("page", blogService.listBlog(pageable,activeTagId));
        model.addAttribute("tags", tags);
        model.addAttribute("activeTagId", activeTagId);
        return "tag";
    }
}
